package beans;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GameFrame
{
	// 变量“声明”在下面
	GameInterface		gamePicture;//游戏界面类的变量
	StartInterface      startPicture;//开始界面类的变量
	
	/******************************* 初始化 在下面 *********************************************************/
	void prepare()//初始化
	{
		startPicture = new StartInterface(this);//初始化开始界面
	}

	/******************************* 鼠标按下 在下面 **********************************************************/
	void mouseClickActivity(MouseEvent e)
	{
		if(startPicture!=null)//开始界面不为空，则执行
		{
			startPicture.mouseClickActivity(mousex, mousey);
		}
		if(gamePicture!=null)//游戏界面不为空，则执行
		{
			gamePicture.mouseClickActivity(mousex, mousey);
			
		}
	}

	/******************************* 鼠标移动 在下面**********************************************************/
	void mouseMoveActivity(MouseEvent e)//鼠标移动活动
	{
		if(gamePicture!=null)
		{
			gamePicture.moveMouse(mousex, mousey);
		}
	}


	/******************************* 界面绘制 在下面**********************************************************/
	void paintPicture(Graphics g)//绘制界面
	{

		if(gamePicture!=null)
		{
			gamePicture.paintPicture(g);//绘制游戏界面
		}
		if(startPicture!=null)
		{
			startPicture.paintPicture(g);//绘制开始界面
		}
	}

	/******************************* 定时处理 在下面**********************************************************/
	void TimingProcess()//定时执行
	{
		if(gamePicture!=null)
		{
			gamePicture.timingProcess();//对游戏界面定时执行
		}
		if(startPicture!=null)
		{
			startPicture.timeProcess();//对开始界面定时执行
		}
	}

	
	
	/******************************** 主运行框架 在下面********************************************************/
	window 	win	= null; //窗口
	timer	time	= null; //定时
	int	mousex;		//<----鼠标的横坐标
	int	mousey;		//<----鼠标的纵坐标

	GameFrame()
	{
		prepare();

		win = new window();
		//窗口 宽+2*立体边, 高+2*立体边+标题栏
		win.setSize(5 * 2 + 800, 600 + 2 * 5 + 25);
		//设定窗口可见性setVisible  true/false
		win.setVisible(true);
		//延时的毫秒
		time = new timer(50);
	}

	class window extends JFrame//窗口继承了JFrame
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		panel pan = null;//面板

		window()//窗口无参构造方法
		{
			pan = new panel(); 
			this.add(pan);
			this.repaint();
		}

		class panel extends JPanel
		{
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			mouseListener mouse = null;//鼠标监听

			panel()//面板无参构造方法
			{
				mouse = new mouseListener();
				this.addMouseListener(mouse);
				this.addMouseMotionListener(mouse);
			}

			class mouseListener extends MouseAdapter implements MouseMotionListener //鼠标监听器
			{
				public void mousePressed(MouseEvent e)//按下鼠标
				{
					mousex = e.getX();
					mousey = e.getY();
					mouseClickActivity(e);
					win.repaint();//刷新页面，重新绘制窗口
				}

				public void mouseMoved(MouseEvent e)//移动鼠标
				{
					mousex = e.getX();
					mousey = e.getY();
					mouseMoveActivity(e);
					win.repaint();//刷新页面，重新绘制窗口
				}
				
			}

			public void paint(Graphics g)
			{
				paintPicture(g);
			}
		}
	}

	class timer implements Runnable//定时器 实现Runnable接口
	{  
		Thread	thread	= null;//操作线程
		long sleepTime;   //刷新间隔时间

		timer(long jianGe)
		{
			this.sleepTime = jianGe;//把50赋值到定时器里面
			thread = new Thread(this);//实例化
			thread.start();//开始跑
		}

		public void run()
		{ //run是游戏框架的类型。计时器必须实现继承的抽象方法Runnable。运行run
			while (true)
			{
				try
				{
					Thread.sleep(sleepTime);//时间暂停50ms

					if (this == time) //如果时间到了
					{
//						User user=new User("小波666");
//						System.out.println("最终成绩："+user.getZombieDieMaxAmount());
						TimingProcess();//定时处理
						win.repaint();//刷新帧
					}
				}
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
		}
	}

	//main主方法 ，主类的“入口方法”
	public static void main(String[] args)
	{
		@SuppressWarnings("unused")
		GameFrame game = new GameFrame();
		
		/*User user1= new User("小波666","111",10);
		User user2= new User("小陈666","222",20);
		User user3= new User("小付666","333",30);*/
		
	}
}
